package com.haier.npt.sso.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.haier.npt.sso.dao.HaierUserDao;
import com.haier.npt.sso.domain.HaierUser;
import com.haier.npt.sso.i18n.SSOMessageCode;
import com.haier.npt.sso.service.SysUserService;
import com.haier.npt.utils.MessageHelper;


@Service
public class SysUserServiceImpl implements SysUserService {

	@Autowired
	private HaierUserDao  sysUserDao ;
	@Autowired
	private MessageHelper messageHelper;
	
	
	
	/**
	 * 根据userid 集合批量查询用户信息
	 * @param userIds
	 * @return
	 */
	@Override
	public List<HaierUser> findUsersByIds(List<Long> userIds){
		if(userIds!=null && userIds.size()>0){
			EntityWrapper<HaierUser> entityWrapper = new EntityWrapper<>(); 
			entityWrapper.in("id", userIds); 
			return this.sysUserDao.selectList(entityWrapper);
		}
		return Collections.emptyList();
	}

	/**
	 * 查询新增用户数
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public Integer getNewAddUsers(Long startDate, Long endDate) {
		Assert.notNull(startDate,messageHelper.getMessage(SSOMessageCode.STARTDATE_NOT_NULL));
		Assert.notNull(endDate,messageHelper.getMessage(SSOMessageCode.ENDDATE_NOT_NULL));
		Date sDate = new Date(startDate);
		Date eDate = new Date(endDate);
		Wrapper<HaierUser> userWrapper = new EntityWrapper<>();
		return sysUserDao.selectCount(userWrapper.ge("registe_date", sDate)
				.and().le("registe_date", eDate));
	}

	/**
	 * 查询最近登录用户数
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public Integer getRecentLoginUsers(Long startDate, Long endDate) {
		Assert.notNull(startDate,messageHelper.getMessage(SSOMessageCode.STARTDATE_NOT_NULL));
		Assert.notNull(endDate,messageHelper.getMessage(SSOMessageCode.ENDDATE_NOT_NULL));
		Date sDate = new Date(startDate);
		Date eDate = new Date(endDate);
		Wrapper<HaierUser> userWrapper = new EntityWrapper<>();
		return sysUserDao.selectCount(userWrapper.ge("last_login_date", sDate)
				.and().le("last_login_date", eDate));
	}

	/**
	 * 获取总用户数
	 * @param endDate
	 * @return
	 */
	@Override
	public Integer getAllUsers(Long endDate) {
		Assert.notNull(endDate,messageHelper.getMessage(SSOMessageCode.ENDDATE_NOT_NULL));
		Date eDate = new Date(endDate);
		Wrapper<HaierUser> userWrapper = new EntityWrapper<>();
		return sysUserDao.selectCount(userWrapper.le("registe_date", eDate)
				.or().isNull("registe_date"));
	}

	/**
	 * 根据用户、邮箱、手机 ,id 查询用户信息 
	 */
	@Override
	public HaierUser findSysUserById(String identity) {
		
		EntityWrapper<HaierUser> entityWrapper = new EntityWrapper<>(); 
		entityWrapper.where("user_name = {0}",identity).orNew("mobile ={0}", identity) .orNew("email ={0}", identity); 
		
		List<HaierUser> users =  this.sysUserDao.selectList(entityWrapper);
	
		if(users!=null && users.size()>0){
			return users.get(0); 
		}
		return null;
	}

	

}
